﻿from datetime import date, timedelta
from DataAccess.DBConnFactory import DBConnFactory
from Config.DBConnConfig import *
import MySQLdb
import cx_Oracle


def insert_date():
	conn = DBConnFactory().get_db_connection('PKEDB')
	
	#for d in xrange(date(2007, 1, 1), date(2012,7,10)):
	one_day = timedelta(days=1)  
	d = date(2007,1,1)
	while d <= date(2012,7,10): 
		conn.cursor().execute("insert into comm_date_list(ref_date) values ('" + d.isoformat() + "')")
		d += one_day
		
	conn.commit()
	
	
	
def transfer_security_static_data():
	des_conn = DBConnFactory().get_db_connection('PKEDB')
	des_cursor = des_conn.cursor()	
	
	src_conn = MySQLdb.connect(host=POSITION_DB_CONN_PARAM[1],
					user=POSITION_DB_CONN_PARAM[2],
					passwd=POSITION_DB_CONN_PARAM[3], 
					db=POSITION_DB_CONN_PARAM[4], 
					charset='utf8')					
	src_cursor = src_conn.cursor()
	
	src_cursor.execute('''select * from comm_security_static_info ''')
	rall = [elem for elem in src_cursor.fetchall()]
	print len(rall)
	
	sql_text = '''insert into comm_security_static_info values(:1,:2)'''
	des_cursor.prepare(sql_text)
	des_cursor.executemany(None, rall)
	des_conn.commit()
	print 'insert complete.'
	return
	
	c = 0
	N = 10
	record_num = len(rall)
	print 'all records number=', record_num
	while(c<record_num):
		end = min(c+N,record_num)
		some = rall[c:end]
		print some
		#return
		
		des_cursor.executemany(None, some)
		des_conn.commit()
		print c
		c = end

	
	# while(r != None):		
		# print r
		
		# s = "insert into comm_security_static_info values('"+r[0]+"','"+r[1]+"')"
		# des_conn.cursor().execute(s)	
		# print 'after execute'
		# des_conn.commit()
		# print 'after commit'
		# r=src_cursor.fetchone()
		

def transfer_fx_price_data():
	des_conn = DBConnFactory().get_db_connection('PKEDB')
	des_cursor = des_conn.cursor()	
	
	src_conn = MySQLdb.connect(host=POSITION_DB_CONN_PARAM[1],
					user=POSITION_DB_CONN_PARAM[2],
					passwd=POSITION_DB_CONN_PARAM[3], 
					db=POSITION_DB_CONN_PARAM[4], 
					charset='utf8')					
	src_cursor = src_conn.cursor()
	
	src_cursor.execute('''select * from comm_fx''')
	rall = [elem for elem in src_cursor.fetchall()]
	print len(rall)
	
	sql_text = '''insert into comm_fx values(:1,:2,:3,:4)'''
	des_cursor.prepare(sql_text)
	des_cursor.executemany(None, rall)
	des_conn.commit()
	print 'insert complete.'
	return
		
	
if __name__ == '__main__':
	#insert_date()
	transfer_fx_price_data()